package com.ming.mallchat.common.chat.service.strategy.msg;

import com.ming.mallchat.common.chat.dao.MessageDao;
import com.ming.mallchat.common.chat.domain.entity.msg.MessageExtra;
import com.ming.mallchat.common.chat.domain.vo.req.msg.TextMsgReq;
import com.ming.mallchat.common.chat.domain.vo.resp.msg.TextMsgResp;
import com.ming.mallchat.common.chat.enums.MessageTypeEnum;
import com.ming.mallchat.common.chat.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class TextMsgHandler extends AbstractMsgHandler<TextMsgReq> {

    @Autowired
    private MessageDao messageDao;

    /**
     * 消息类型
     */
    @Override
    MessageTypeEnum getMsgTypeEnum() {
        return MessageTypeEnum.TEXT;
    }

    @Override
    protected void saveMsg(Message msg, TextMsgReq body) {
        MessageExtra messageExtra = Optional.ofNullable(msg.getExtra()).orElse(new MessageExtra());
        Message update = new Message();
        update.setId(msg.getId());
        update.setContent(body.getContent());
        update.setExtra(messageExtra);
        messageDao.updateById(update);
    }

    /**
     * 展示消息
     *
     * @param msg
     */
    @Override
    public Object showMsg(Message msg) {
        TextMsgResp resp = new TextMsgResp();
        resp.setContent(msg.getContent());
        resp.setUrlContentMap(Optional.ofNullable(msg.getExtra()).map(MessageExtra::getUrlContentMap).orElse(null));
        resp.setAtUidList(Optional.ofNullable(msg.getExtra()).map(MessageExtra::getAtUidList).orElse(null));
        return resp;
    }

    @Override
    protected void checkMsg(TextMsgReq body, Long roomId, Long uid) {
        super.checkMsg(body, roomId, uid);
    }

    /**
     * 被回复时——展示的消息
     *
     * @param msg
     */
    @Override
    public Object showReplyMsg(Message msg) {
        return msg.getContent();
    }

    /**
     * 会话列表——展示的消息
     *
     * @param msg
     */
    @Override
    public String showContactMsg(Message msg) {
        return msg.getContent();
    }
}
